#include"SqList.h"
void SLPrint(SL s)
{
	for (int i = 0; i < s.size; i++)
	{
		printf("%d ", s.a[i]);
	}
	printf("\n");
}
void SLInit(pSL ps)
{
	assert(ps);
	ps->a = NULL;
	ps->capacity = ps->size = 0;
}
void SLDestroy(pSL ps)
{
	assert(ps);
	free(ps->a);
	ps->capacity = ps->size = 0;
}
void SLSpaceApply(pSL ps)
{
	if (ps->capacity == ps->size)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SLDataType* tmp = (SLDataType*)realloc(ps->a, sizeof(SLDataType) * newcapacity);
		if (tmp == NULL)
		{
			perror("realloc fail!!");
			return;
		}
		ps->capacity = newcapacity;
		ps->a = tmp;
	}
}
void SLPushBack(pSL ps, SLDataType x)
{
	assert(ps);
	SLSpaceApply(ps);
	ps->a[ps->size++] = x;
}
void SLPushFront(pSL ps, SLDataType x)
{
	assert(ps);
	SLSpaceApply(ps);
	for (int i = ps->size; i > 0; i--)
	{
		ps->a[i] = ps->a[i - 1];
	}
	ps->a[0] = x;
	ps->size++;
}
void SLPopBack(pSL ps)
{
	assert(ps);
	assert(ps->size);
	ps->size--;
}
void SLPopFront(pSL ps)
{
	assert(ps);
	assert(ps->size);
	for (int i = 0; i < ps->size - 1; i++)
	{
		ps->a[i] = ps->a[i + 1];
	}
	ps->size--;
}
void SLPushPos(pSL ps, int pos, SLDataType x)
{
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);
	SLSpaceApply(ps);
	if (ps->size == 0)
	{
		SLPushBack(ps, x);
	}
	else 
	{
		for (int i = ps->size; i > pos; i--)
		{
			ps->a[i] = ps->a[i - 1];
		}
		ps->a[pos] = x;
		ps->size++;
	}
}
void SLPopPos(pSL ps, int pos)
{
	assert(ps);
	assert(ps->size);
	assert(pos >= 0 && pos < ps->size);
	for (int i = pos; i < ps->size - 1; i++)
	{
		ps->a[i] = ps->a[i + 1];
	}
	ps->size--;
}